package com.tangcan.blog.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.tangcan.blog.Entity.BlogContent;
import com.tangcan.blog.Entity.Comment;
import com.tangcan.blog.Mapper.CommentMapper;
import com.tangcan.blog.Utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@EnableAutoConfiguration
public class CommentService {
    @Autowired
    CommentMapper commentMapper;

    TokenUtils tokenUtils;
    public boolean isAdmin(String token)
    {
        Map tokenInfo = tokenUtils.getTokenInfo(token);
        String authority = tokenInfo.get("authority").toString().replace("\"","");
        if (authority.equals("admin"))
        {
            return true;
        }
        else {
            return false;
        }
    }
    public JSONObject GetAll(){
        JSONObject resJson = new JSONObject();
        List<Comment> CommentList = commentMapper.GetAll();
        if (!CommentList.isEmpty())
        {
            resJson.put("code","ok");
            resJson.put("commentList",CommentList);
        }
        else {
            resJson.put("code","err");
            resJson.put("info","Null err");

        }
        return resJson;

    }
    public JSONObject GetByBlogId(String data)
    {
        JSONObject resJson = new JSONObject();
        JSONObject json = JSON.parseObject(data);
        int BlogId = json.getInteger("blogId");
        List<Comment> commentList = commentMapper.GetBlogComment(BlogId);
        if (!commentList.isEmpty())
        {
            resJson.put("code","ok");
            resJson.put("commentList",commentList);

        }
        else {
            resJson.put("code","ok");
            resJson.put("commentList",null);
        }
        return resJson;
    }
    public JSONObject GetById(String data)
    {
        JSONObject resJson = new JSONObject();
        JSONObject json = JSON.parseObject(data);
        int Id = json.getInteger("commentId");
        Comment comment = commentMapper.GetCommentById(Id);
        resJson.put("code","ok");
        resJson.put("comment",comment);
        return resJson;

    }

    public JSONObject AddComment(String data){
        JSONObject json = JSON.parseObject(data, Feature.OrderedField);
        JSONObject resJson = new JSONObject();
        String token = json.getString("token");
        if (tokenUtils.verify(token) && isAdmin(token))
        {
            if (!json.getString("comment").isEmpty())
            {
                Comment REQcomment = JSONObject.parseObject(json.getString("comment"),Comment.class);
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                String date = df.format(new Date());
                REQcomment.setTime(date);
                commentMapper.AddComment(REQcomment);
                resJson.put("code","ok");
                resJson.put("info","ok");
                return resJson;
            }
            else {
                resJson.put("code","err");
                resJson.put("info","comment为空");
                return resJson;
            }

        }
        else
        {
            resJson.put("code","err");
            resJson.put("info","鉴权失败");
            return resJson;
        }
    }

    public JSONObject UpdateComment(String data)
    {
        JSONObject json = JSON.parseObject(data, Feature.OrderedField);
        JSONObject resJson = new JSONObject();
        String token = json.getString("token");
        if (tokenUtils.verify(token) && isAdmin(token))
        {
            if (!json.getString("comment").isEmpty())
            {
                Comment comment = JSONObject.parseObject(json.getString("comment"),Comment.class);
                int eff =  commentMapper.UpdateComment(comment);
                if (eff >0)
                {
                    resJson.put("code","ok");
                    resJson.put("info","ok");
                    return resJson;
                }
                else {
                    resJson.put("code","err");
                    resJson.put("info","增加失败");
                    return resJson;
                }
            }else
            {
                resJson.put("code","err");
                resJson.put("info","comment为空");
                return resJson;
            }
        }
        else
        {
            resJson.put("code","err");
            resJson.put("info","鉴权失败");
            return resJson;
        }
    }
    public JSONObject RemoveComment(String data)
    {
        JSONObject json = JSON.parseObject(data, Feature.OrderedField);
        JSONObject resJson = new JSONObject();
        String token = json.getString("token");
        if (tokenUtils.verify(token) && isAdmin(token))
        {
            int id  = json.getInteger("id");
            int efc = commentMapper.RemoveComment(id);
            if (efc > 0 )
            {
                resJson.put("code","ok");
                resJson.put("info","ok");
                return resJson;
            }
            else {
                resJson.put("code","err");
                resJson.put("info","删除失败");
                return resJson;
            }
        }
        else
        {
            resJson.put("code","err");
            resJson.put("info","鉴权失败");
            return resJson;
        }
    }

}
